package com.lizardmind.haofudoctor.ui.studio.serviceSetting

import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.view.View
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.getDataBinding
import com.linxiao.framework.util.ke
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivityServiceSettingBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.*
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.include_title_right_text.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * 服务设置 界面
 * Created by Extends on 2017/11/27 14:38
 */
class ServiceSettingActivity:BaseActivity() {
    private val binding by lazy { getDataBinding<ActivityServiceSettingBinding>(R.layout.activity_service_setting) }
    override fun onCreatRootView(): View = binding.root

    override fun onInitView(savedInstanceState: Bundle?) {
        //初始化title
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text = "服务设置"
        tvRight.text = "保存"
        tvRight.onClick {
            observe {
                postApi<Request<String>>(hashMapOf(
                        "action" to "edit_service",
                        "token" to DoctorInfo.token,
                        "data" to jsonOf(
                                "welcome" to binding.vm!!.welcome.get(),
                                "disturb" to if(binding.assDisturb.isChecked) 1 else 0
                        ).formatToZY()
                ), Consumer {
                    it.dispose {
                        toast(it)
                    }
                })
            }
        }

        binding.apply {
            vm = ViewModelProviders.of(this@ServiceSettingActivity).get(ServiceSettingVM::class.java)
            lifecycle.addObserver(vm!!)

            assSetTime.onClick { startActivity<TimeSettingActivity>("week" to binding.vm!!.week.get(),"time" to binding.vm!!.time.get()) }
            assCharge.onClick { startActivity<ChargeSettingActivity>("supplement" to binding.vm!!.supplement.get(),"charge" to binding.vm!!.charge.get()) }
        }

        /**
         * 获取服务设置的数据
         */
        observe {
            postApi<Request<ServiceSettingBean>>(hashMapOf(
                    "action" to "service",
                    "token" to DoctorInfo.token
            ), Consumer {
                it.dispose {
                    binding.vm!!.initData(it)
                }
            },context = this)
        }
    }
}